package com.bluestone.assertor;

import java.io.BufferedInputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

import org.apache.log4j.spi.LoggingEvent;

import com.bluestone.util.Util;
import com.voxeo.logging.EncodedPatternLayout;

public class LoggingSocket implements Runnable {
	Socket socket;
	ObjectInputStream ois;
	LoggingAssertor assertor;
	

	public LoggingSocket(Socket socket,LoggingAssertor assertor) {
		this.socket = socket;
		this.assertor = assertor;
		try {
			ois = new ObjectInputStream(new BufferedInputStream(socket
					.getInputStream()));
		} catch (Exception e) {
			Util.error("Could not open ObjectInputStream to " + socket);
		}
	}

	public void run() {
		

		try {
			while (true) {
				// read an event from the wire
				LoggingEvent event = (LoggingEvent) ois.readObject();	
				//EncodedPatternLayout layout = new EncodedPatternLayout();
				//String message = layout.format(event);
				assertor.putData(event);
			}

		} catch (Exception e) {
			Util.error("Unexpected exception. Closing conneciton.");
		}

		try {
			ois.close();
		} catch (Exception e) {
			Util.info("Could not close connection.");
		}

	}

}
